package com.commerceE.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.commerceE.common.exception.ServiceException;
import com.commerceE.domain.Category;
import com.commerceE.domain.Products;
import com.commerceE.service.CategoryService;
import com.commerceE.service.ProductService;
import com.commerceE.util.Constant;

@SessionAttributes({ "productList" })
@Controller
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService categoryService;

	@ModelAttribute("productList")
	public PagedListHolder populatePagedListHolder() {
		return new PagedListHolder();
	}

	@RequestMapping(value = "/listAll", method = RequestMethod.GET)
	public String listAll(boolean flag, String pageType,
			@ModelAttribute("productList") PagedListHolder productList,
			Model model) {
		try {
			if (flag) {
				productList = new PagedListHolder(
						productService.getAllListOfProduct());
				productList.setPageSize(Constant.pageNumber);
				model.addAttribute("productList", productList);
			} else {
				if ("next".equals(pageType)) {
					productList.nextPage();
				} else if ("previous".equals(pageType)) {
					productList.previousPage();
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return "productList";
	}

	@RequestMapping(value = "/addNewProduct_Form", method = RequestMethod.GET)
	public String addNewCategory_Form(Model model) {
		try {
			List<Category> searchCategoryList = categoryService
					.getAllListOfCategory();
			model.addAttribute("searchCategoryList", searchCategoryList);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		model.addAttribute(new Products());
		return "newProduct";
	}

	@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
	public String addCategory(@Valid Products product, BindingResult result,
			Model model) {
		if (result.hasErrors()) {
			return "newProduct";
		}
		try {
			productService.insertProduct(product);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return "redirect:listAll.do?flag=true";
	}

	@RequestMapping(value = "/productDetail", method = RequestMethod.GET)
	public String prodcutDetail(String productId, Model model) {
		try {
			Products product = productService.getProductById(productId);
			model.addAttribute("product", product);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return "productDetail";
	}
}
